package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.controlserver.SmartInfo;
import com.ruoyi.system.controlserver.SmartQuary;
import com.ruoyi.system.domain.SrswDevices;
import com.ruoyi.system.service.ISmartScreenService;
import com.ruoyi.system.service.ISrswDevicesService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 智慧屏
 *
 * @author chenjy
 */
@RestController
@RequestMapping("/system/smartScreen")
public class SmartScreenController extends BaseController {
    @Autowired
    private ISmartScreenService smartScreenService;

    @Autowired
    private ISrswDevicesService srswDevicesService;
    @Autowired
    private ISysDeptService deptService;

    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }

    @PostMapping("/udpateDept")
    public AjaxResult udpateDept(@RequestBody SrswDevices srswDevices)
    {
        if(srswDevices.getDeptId() == null){
            return AjaxResult.warn("部门不能为空");
        }
        if(StringUtils.isBlank(srswDevices.getDeviceNo())){
            return AjaxResult.warn("设备编号不能为空");
        }
        SrswDevices srswDeviceInfo = new SrswDevices();
        srswDeviceInfo.setDeptId(srswDevices.getDeptId());
        srswDeviceInfo.setDeviceNo(srswDevices.getDeviceNo());
        return success(srswDevicesService.updateSrswDevices(srswDeviceInfo));
    }

    /**
     * 获取所有智慧屏ip
     */
    @GetMapping("/list")
    public AjaxResult list( SmartInfo info) {
        List<SmartInfo> infos = smartScreenService.list(info);
        infos = infos.stream().sorted(Comparator.comparing(SmartInfo::getIp)).collect(Collectors.toList());
        return success(infos);
    }

    /**
     * 获取所有智慧屏ip
     */
    @GetMapping("/newList")
    public AjaxResult newList() {
        List<SmartInfo> smartInfoList = smartScreenService.newList();
        return success(smartInfoList);
    }

    /**
     * 下发指令
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/instruct")
    public AjaxResult instruct(@Validated @RequestBody  SmartInfo info) {

        if(StringUtils.isBlank(info.getDeviceNo())){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        //判断info是否在infos中
        if(infos.stream().noneMatch(info1 -> info1.getDeviceNo().equals(info.getDeviceNo()))){
            return AjaxResult.error("智慧屏离线");
        }
        try {
            smartScreenService.instruct(info);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            return AjaxResult.success("操作失败");
        }

    }

    /**
     * 批量亮屏
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchScreenOn")
    public AjaxResult batchScreenOn(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setScreenStatus(true);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
            cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量息屏
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchScreenOff")
    public AjaxResult batchScreenOff(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setScreenStatus(false);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量重启
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchRestart")
    public AjaxResult batchRestart(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setRestart(false);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量关机
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchPowerOff")
    public AjaxResult batchPowerOff(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setPowerStatus(true);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量开启pc
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchOpsOn")
    public AjaxResult batchOpsOn(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setOpsStatus(true);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量关闭pc
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchOpsOff")
    public AjaxResult batchOpsOff(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setOpsStatus(false);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量静音
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchMute")
    public AjaxResult batchMute(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setMuteStatus(true);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量解除静音
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchUnMute")
    public AjaxResult batchUnMute(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setMuteStatus(false);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量设置音量
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchVoice")
    public AjaxResult batchVoice(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(smartQuary.getDeviceNos()).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setVoice(smartQuary.getVoice());
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

    /**
     * 批量切换信号源
     */
    @Log(title = "智慧屏控制", businessType = BusinessType.INSTRUCT)
    @PostMapping("/batchSource")
    public AjaxResult batchSource(@RequestBody SmartQuary smartQuary) {
        if(smartQuary.getDeviceNos()==null || smartQuary.getDeviceNos().length==0){
            return AjaxResult.error("请选择智慧屏");
        }
        String [] deviceNos = smartQuary.getDeviceNos();
        String source = smartQuary.getSource();
        List<SmartInfo> infos = smartScreenService.list(new SmartInfo());
        final List<SmartInfo> targets = infos.stream().filter(info -> Arrays.asList(deviceNos).contains(info.getDeviceNo()))
                .collect(Collectors.toList());
        CountDownLatch cdl = new CountDownLatch(targets.size());
        for (SmartInfo target : targets) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setDeviceNo(target.getDeviceNo());
            smartInfo.setSource(source);
            new Thread(() -> {smartScreenService.instruct(smartInfo);
                cdl.countDown();}).start();
        }
        try {
            cdl.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {

        }
        return AjaxResult.success("在线设备操作成功");
    }

}
